package com.goods.business.converter;

import com.goods.business.service.*;
import com.goods.common.model.business.*;
import com.goods.common.response.ResponseBean;
import com.goods.common.utils.CategoryTreeBuilder;
import com.goods.common.utils.JWTUtils;
import com.goods.common.vo.business.*;
import com.goods.common.vo.system.PageVO;
import io.swagger.models.auth.In;
import org.apache.shiro.authc.AuthenticationToken;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.*;

@RestController
@RequestMapping("/business")
public class BusinessController {

    // 获取分页数据
    @GetMapping("/productCategory/categoryTree")
    public ResponseBean getCategoryTree(@RequestParam(value = "pageNum",required = false) Integer pageNum,@RequestParam(value = "pageSize",required = false) Integer pageSize){

        PageVO<ProductCategory> productCategoryList = businessService.getCategoryTree(pageNum,pageSize);
        return ResponseBean.success(productCategoryList);
    }

    // 加载父级分类数据
    @GetMapping("/productCategory/getParentCategoryTree")
    public ResponseBean getParentCategoryTree(){
        List<ProductCategoryTreeNodeVO> parentCategoryTree = businessService.getParentCategoryTree();
        return ResponseBean.success(parentCategoryTree);
    }

    // 添加分类
    @PostMapping("/productCategory/add")
    public ResponseBean add(@RequestBody ProductCategoryVO productCategoryVO){
        ProductCategory productCategory = new ProductCategory();
        BeanUtils.copyProperties(productCategoryVO,productCategory);
        businessService.add(productCategory);
        return ResponseBean.success();
    }

    // 修改分类-回显数据-根据id获取分类信息
    @RequestMapping("/productCategory/edit/{id}")
    public ResponseBean edit(@PathVariable Long id){
        ProductCategoryVO productCategoryVO =  new ProductCategoryVO();
        ProductCategory productCategory = businessService.edit(id);
        BeanUtils.copyProperties(productCategory,productCategoryVO);
        return ResponseBean.success(productCategoryVO);
    }

    // 修改分类
    @PutMapping("/productCategory/update/{id}")
    public ResponseBean update(@PathVariable Long id,@RequestBody ProductCategoryVO productCategoryVO){
        ProductCategory productCategory = new ProductCategory();
        BeanUtils.copyProperties(productCategoryVO,productCategory);
        businessService.update(id,productCategory);
        return ResponseBean.success();
    }

    // 删除分类
    @DeleteMapping("/productCategory/delete/{id}")
    public ResponseBean delete(@PathVariable Long id){
        businessService.delete(id);
        return ResponseBean.success();
    }

    // 物流来源分页展示
    @GetMapping("/supplier/findSupplierList")
    public ResponseBean findSupplierList(@ModelAttribute SupplierQueryVO supplierQueryVO){
        PageVO<Supplier> pageVO = supplierService.findSupplierList(supplierQueryVO);
        return ResponseBean.success(pageVO);
    }

    // 添加物流来源
    @PostMapping("/supplier/add")
    public ResponseBean add(@RequestBody Supplier supplier){
        supplierService.add(supplier);
        return ResponseBean.success();
    }

    // 修改物流来源-根据id查询
    @GetMapping("/supplier/edit/{id}")
    public ResponseBean editSupplier(@PathVariable Long id){
        Supplier supplier = supplierService.edit(id);
        return ResponseBean.success(supplier);
    }

    // 修改物流来源
    @PutMapping("/supplier/update/{id}")
    public ResponseBean updateSupplier(@PathVariable Long id,@RequestBody Supplier supplier){
        supplierService.update(supplier);
        return ResponseBean.success();
    }

    // 删除物流来源
    @DeleteMapping("/supplier/delete/{id}")
    public ResponseBean deleteSupplier(@PathVariable Long id){
        supplierService.delete(id);
        return ResponseBean.success();
    }

    // 展示物资资料
    @GetMapping("/product/findProductList")
    public ResponseBean findProductList(@ModelAttribute ListQueryVo listQueryVo){
        PageVO<Product> list = listService.findProductList(listQueryVo);
        return ResponseBean.success(list);
    }

    // 添加物资资料
    @PostMapping("/product/add")
    public ResponseBean addList(@RequestBody ProductVO productVO){

        Product product = new Product();
        BeanUtils.copyProperties(productVO,product);
        // 分类id
        Long[] categoryKeys = productVO.getCategoryKeys();
        // 对product得封装
        product.setOneCategoryId(categoryKeys[0]);
        product.setTwoCategoryId(categoryKeys[1]);
        product.setThreeCategoryId(categoryKeys[2]);
        product.setCreateTime(new Date());
        product.setModifiedTime(new Date());
        product.setStatus(2);
        product.setPNum(UUID.randomUUID().toString());
        listService.add(product);
        return ResponseBean.success();
    }

    // 编辑物资资料-根据id查询
    @GetMapping("product/edit/{id}")
    private ResponseBean editList(@PathVariable Long id){
        Product product = listService.editList(id);
        ProductVO productVO = new ProductVO();
        BeanUtils.copyProperties(product,productVO);
        return ResponseBean.success(productVO);
    }

    // 编辑物资资料
    @PutMapping("product/update/{id}")
    private ResponseBean updateList(@PathVariable Long id,@RequestBody Product product){
        listService.updateList(id,product);
        return ResponseBean.success();
    }

    // 物资放入回收站  status:0物资正常,1:物资回收,2:物资审核中
    @PutMapping("product/remove/{id}")
    private ResponseBean remove(@PathVariable Long id){
        listService.updateStatus(id,1);
        return ResponseBean.success();
    }

    // 物资从回收站恢复  status:0物资正常,1:物资回收,2:物资审核中
    @PutMapping("product/back/{id}")
    private ResponseBean back(@PathVariable Long id){
        listService.updateStatus(id,0);
        return ResponseBean.success();
    }

    // 审核物资通过
    @PutMapping("product/publish/{id}")
    private ResponseBean publish(@PathVariable Long id){
        listService.updateStatus(id,0);
        return ResponseBean.success();
    }

    // 删除物资
    @DeleteMapping("product/delete/{id}")
    private ResponseBean deleteList(@PathVariable Long id){
        listService.deleteList(id);
        return ResponseBean.success();
    }

    // 入库信息展示
    @GetMapping("inStock/findInStockList")
    private ResponseBean findInStockList(@ModelAttribute FindInStockQueryVo findInStockQueryVo){
        PageVO<InStockVO> pageVO = inStockService.findInStockList(findInStockQueryVo);
        return ResponseBean.success(pageVO);
    }

    // 查询入库信息明细
    @GetMapping("inStock/detail/{id}")
    private ResponseBean detailInStock(@PathVariable Long id,Integer pageNum){
        InStockDetailVO inStockDetailVO = inStockService.detailInStock(id,pageNum);
        return ResponseBean.success(inStockDetailVO);
    }

    // 进入回收站 status = 1
    @PutMapping("inStock/remove/{id}")
    private ResponseBean removeInStock(@PathVariable Long id){
        inStockService.changeStatus(id,1);
        return ResponseBean.success();
    }

    // 从回收站恢复 status = 0
    @PutMapping("inStock/back/{id}")
    private ResponseBean backInStock(@PathVariable Long id){
        inStockService.changeStatus(id,0);
        return ResponseBean.success();
    }

    // 删除入库信息
    @GetMapping("inStock/delete/{id}")
    private ResponseBean deleteInStock(@PathVariable Long id){
        inStockService.deleteInStock(id);
        return ResponseBean.success();
    }

    // 分页查询商品
    @GetMapping("product/findProducts")
    public ResponseBean findProducts(@ModelAttribute ListQueryVo listQueryVo){
        PageVO<Product> list = listService.findProductList(listQueryVo);
        return ResponseBean.success(list);
    }

    // 获取所有来源数据-不分页
    @GetMapping("supplier/findAll")
    public ResponseBean findAllSupplier(){
        List<Supplier> supplier = supplierService.findAllSupplier();
        List<SupplierVO> supplierVOS = new ArrayList<>();
        for (Supplier s : supplier) {
            SupplierVO supplierVO = new SupplierVO();
            BeanUtils.copyProperties(s,supplierVO);
            supplierVOS.add(supplierVO);
        }
        return ResponseBean.success(supplierVOS);
    }

    // 新增入库
    @PostMapping("inStock/addIntoStock")
    public ResponseBean addIntoStock(@RequestBody InStockVO inStockVO, ServletRequest request){
        HttpServletRequest httpServletRequest= (HttpServletRequest) request;
        String token = httpServletRequest.getHeader("Authorization");
        // 解密获得username，用于和数据库进行对比
        String username = JWTUtils.getUsername(token);
        inStockService.addIntoStock(inStockVO,username);
        return ResponseBean.success();
    }

    // 审核通过入库信息
    @PutMapping("inStock/publish/{id}")
    public ResponseBean publishInStock(@PathVariable Long id){
        inStockService.changeStatus(id,0);
        return ResponseBean.success();
    }

    // 查询商品的库存
    @GetMapping("product/findProductStocks")
    public ResponseBean findProductStocks(Integer pageNum,Integer pageSize){
        PageVO<ProductStockVO>  productStocks = productStockService.findProductStocks(pageNum,pageSize);
        return ResponseBean.success(productStocks);
    }
    // 查询所有的库存
    @GetMapping("product/findAllStocks")
    public ResponseBean findAllStocks(Integer pageNum,Integer pageSize){
        List<ProductStockVO> list = productStockService.findAllStocks(pageNum,pageSize);
        return ResponseBean.success(list);
    }
    // 物资去处展示
    @GetMapping("consumer/findConsumerList")
    public ResponseBean findConsumerList(@ModelAttribute FindConsumerQueryVo consumerQueryVo){
        PageVO<ConsumerVO> pageVO = consumerService.findConsumerList(consumerQueryVo);
        return ResponseBean.success(pageVO);
    }
    // 添加物资去处
    @PostMapping("consumer/add")
    public ResponseBean addConsumer(@RequestBody ConsumerVO consumerVO){
        consumerService.addConsumer(consumerVO);
        return ResponseBean.success();
    }
    // 编辑物资去处-根据id查询
    @GetMapping("consumer/edit/{id}")
    public ResponseBean editConsumer(@PathVariable Long id){
        Consumer consumer = consumerService.editConsumer(id);
        return ResponseBean.success(consumer);
    }
    // 编辑物资去处
    @PutMapping("consumer/update/{id}")
    public ResponseBean updateConsumer(@PathVariable Long id,@RequestBody Consumer consumer){
        consumerService.updateConsumer(id,consumer);
        return ResponseBean.success();
    }
    // 删除物资去处
    @DeleteMapping("consumer/delete/{id}")
    public ResponseBean deleteConsumer(@PathVariable long id){
        consumerService.deleteConsumer(id);
        return ResponseBean.success();
    }
    // 物资出库展示
    @GetMapping("outStock/findOutStockList")
    public ResponseBean findOutStockList(@ModelAttribute FindOutStockQueryVo findOutStockQueryVo){
        PageVO<OutStockVO> vo = outStockService.findOutStockList(findOutStockQueryVo);
        return ResponseBean.success(vo);
    }
    // 物资出库明细展示
    @GetMapping("outStock/detail/{id}")
    public ResponseBean detailOutStockList(@PathVariable Long id,Integer pageNum,ServletRequest request){
        // 从请求域中获取token 获取用户名(当前操作者)
        HttpServletRequest httpServletRequest = (HttpServletRequest)request;
        String token = httpServletRequest.getHeader("Authorization");
        String username = JWTUtils.getUsername(token);
        OutStockDetailVO outStockDetailVO = outStockService.detailOutStockList(id,pageNum,username);
        return ResponseBean.success(outStockDetailVO);
    }
    // 从回收站还原 status = 0 正常
    @PutMapping("outStock/back/{id}")
    public ResponseBean backOutStockList(@PathVariable Long id){
        outStockService.changeOutStockListStatus(id,0);
        return ResponseBean.success();
    }
    // 移动到回收站 status = 1 进入回收站
    @PutMapping("outStock/remove/{id}")
    public ResponseBean removeOutStockList(@PathVariable Long id){
        outStockService.changeOutStockListStatus(id,1);
        return ResponseBean.success();
    }
    // 审核通过 status = 0 正常
    @PutMapping("outStock/publish/{id}")
    public ResponseBean publishOutStockList(@PathVariable Long id){
        outStockService.changeOutStockListStatus(id,0);
        return ResponseBean.success();
    }
    // 删除出库
    @GetMapping("outStock/delete/{id}")
    public ResponseBean deleteOutStockList(@PathVariable Long id){
        outStockService.deleteOutStockList(id);
        return ResponseBean.success();
    }
    // 添加入库
    @PostMapping("outStock/addOutStock")
    public ResponseBean addOutStock(@RequestBody OutStockVO outStockVO,ServletRequest request){
        HttpServletRequest httpServletRequest= (HttpServletRequest) request;
        String token = httpServletRequest.getHeader("Authorization");
        // 解密获得username，用于和数据库进行对比
        String username = JWTUtils.getUsername(token);
        outStockService.addOutStock(outStockVO,username);
        return ResponseBean.success();
    }
    // 健康打卡(新增)
    @PostMapping("health/report")
    public ResponseBean healthReport(@RequestBody HealthVO healthVO,ServletRequest request){
        // 获取用户名
        HttpServletRequest httpServletRequest= (HttpServletRequest) request;
        String token = httpServletRequest.getHeader("Authorization");
        String username = JWTUtils.getUsername(token);
        healthService.healthReport(healthVO,username);
        return ResponseBean.success();
    }
    // 查询健康打卡
    @GetMapping("health/history")
    public ResponseBean healthHistory(Integer pageNum,Integer pageSize){
        PageVO<Health> pageVO = healthService.healthHistory(pageNum,pageSize);
        return ResponseBean.success(pageVO);
    }
    // 查询当前用户今日是否打卡
    @GetMapping("health/isReport")
    public ResponseBean healthIsReport(ServletRequest request){
        // 获取用户名
        HttpServletRequest httpServletRequest= (HttpServletRequest) request;
        String token = httpServletRequest.getHeader("Authorization");
        String username = JWTUtils.getUsername(token);
        Boolean isReport = healthService.healthIsReport(username);
        return ResponseBean.success(isReport);
    }


    @Autowired
    private HealthService healthService;
    @Autowired
    private OutStockService outStockService;
    @Autowired
    private ConsumerService consumerService;
    @Autowired
    private ProductStockService productStockService;
    @Autowired
    private InStockService inStockService;
    @Autowired
    private ListService listService;
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private BusinessService businessService;
}
